package com.cxhome.cxinfox.Auditing;

import org.apache.log4j.Logger;

import com.cxhome.cxinfox.accountRecord.AccountService;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.traffic.managent.base.BaseService;
import com.traffic.managent.base.Paginator;
import com.traffic.managent.model.product.Auditing;
import com.traffic.managent.model.system.User;
import com.traffic.managent.util.SQLUtils;

/**
 * 审核业务处理类。
 * 
 */
public class AuditingService extends BaseService {

	private final static Logger log = Logger.getLogger(AuditingService.class);
	public static final AuditingService service = new AuditingService();

	public Page<Auditing> paginate(Auditing model, Paginator paginator, String orderSql) {
		SQLUtils sql = new SQLUtils(" from cx_auditing ca where 1=1");

		if (model.getAttrValues().length != 0) {
			sql.whereLike("status", model.getStr("status"));
			sql.whereLike("type", model.getStr("type"));
		}
		sql.append(orderSql);

		return Auditing.dao.paginate(paginator, "select ca.*  ", sql.toString());

	}

	@Before(Tx.class)
	public void saveOrUpdate(String ids, String status, User user) {

		try {
			if ("2".equals(status)) {// 驳回
				Auditing.dao.updateStatus2(ids, user.getUserName(), status);
			} else {// 通过,查看类型
				Auditing auditing = Auditing.dao.findById(ids);
				Auditing.dao.updateStatus2(ids, user.getUserName(), status);
				String type = auditing.getStr("type");
				if ("0".equals(type)) {// 加款
					AccountService.service.chargeR(auditing.getStr("auditingIds"),
							String.valueOf(auditing.getBigDecimal("chargeAmount")),
							String.valueOf(auditing.getBigDecimal("giftAmount")), auditing.getStr("remark"), user);

				} else if ("1".equals(type)) {// 授信
					AccountService.service.setCredit(auditing.getStr("auditingIds"),
							String.valueOf(auditing.getBigDecimal("creditAmount")),
							String.valueOf(auditing.getBigDecimal("warnAmount")), user);
				}
			}

		} catch (Exception e) {
			log.error("操作异常：" + e);
			e.printStackTrace();
		}

	}

}
